// pages/list/list.js
//引入 request
const http = require("../../utils/http");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jijieArr: [],
    searchKey: "",
    endtype: false //false 没到底 ,true 到底了
  },
  //定义自由数据,自由数据的优势，不需要双向绑定，不需要调用 setData，性能更优
  customData: {
    page: 1, //当前页
  },

  //获取数据
  getData() {
    http.request("requirement/request/getMatchedProducts", {
      curPagePath: "/wx/customer/classic/search.html?pageid=0piQDrywbusu9y3CXGJj",
      keyWord: this.data.searchKey,
      pageNow: this.customData.page,
      pageSize: 10,
      product_type: 1
    }, "post").then(res => {
      this.setData({
        jijieArr: res.data.matchedProducts
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      searchKey: options.searchKey
    })
    this.getData();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新");
    //重新获取数据
    this.getData();
    //停止下拉
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.endtype) {
      this.customData.page++;
      http.request("requirement/request/getMatchedProducts", {
        curPagePath: "/wx/customer/classic/search.html?pageid=0piQDrywbusu9y3CXGJj",
        keyWord: "中国",
        pageNow: this.customData.page,
        pageSize: 10,
        product_type: 1
      }, "post").then(res => {
        const data = res.data.matchedProducts;
        if (data.length == 0) {
          this.setData({
            endtype: true
          })
        } else {
          //4.将新数据追加到原数据的后面
          const oldData = this.data.jijieArr;
          const newData = oldData.concat(data);
          this.setData({
            jijieArr: newData
          })
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})